{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 10 - Logistic Regression\n", "\n", "The Challenger Space Shuttle tragically explored in 1986, killing all astronauts on board. The explosion was shown to have been caused by an O-ring failure, likely due to cold temperatures the day of the launch (and also poor engineering that allowed this failure to cause such catastrophy).\n", "\n", "This lab will use experimental data from tests on whether O-rings failed at different temperatures. The data set can be downloaded [here](http://comet.lehman.cuny.edu/owen/teaching/mat328/chall.txt)\n", "\n", "Some of this lab is based off the Harvard Data Science CS109 Lab 4, Fall 2015." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import numpy as np\n", "import statsmodels.formula.api as smf\n", "\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read the data into a dataframe." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a scatter plot with temperature on the x axis and failure on the y axis. What do you notice?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will now use statsmodel to fit a logistic regression model to the data. Notice that the code is similar to when we fit a linear regression model to the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "logit_model = smf.logit('Failure ~ Temperature',data).fit()\n", "logit_model.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is there an R-squared value in the summary? What is the formula for the model?\n", "\n", "There is another way to get the model parameters:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "logit_model.params" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use these parameters to graph the model equation on the data. \n", "\n", "First, create 200 evenly spaced x values (look at the data to see what their range should be): " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we can compute $\\beta_0 + \\beta_1 x$ for all of these x values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "p = logit_model.params\n", "reg = p['Intercept'] + x*p['Temperature']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally we can plug `reg` into the logistic equation to get the y values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = np.exp(reg)/(1 + np.exp(reg))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot another scatter plot of the data, plus the plot of our calculated x and y values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One way to understand how well our model works is to make a *confusion table* or *confusion matrix*, which counts how many of each type of error there are. We can create the table using the `pred_table()` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "logit_model.pred_table()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The confusion matrix can be read as follows:\n", " \n", " predicted\n", " | 0 | 1 |\n", " --------------------------------\n", "observed | 0 | true negative | false positive\n", " | 1 | false negative | true positive\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How many correct predictions did the model make? What kind of wrong predictions did the model make?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pima (Akimel Oʼodham) Indian Diabetes data\n", "\n", "The Akimel O'odham people, who were also known as the Pima Indians since European colonization of the US, currently have a high prevalence of diabetes. A data set of different possible diabetes indicators and whether the person has diabetes is on [Kaggle](ttps://www.kaggle.com/uciml/pima-indians-diabetes-database) or available [here](http://comet.lehman.cuny.edu/owen/teaching/mat328/diabetes.csv).\n", "\n", "Read in the dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot a scatter plot of glucose vs. diabetes." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fit a logistic regression model to this data." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot the model equation on top of your scatter plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What does the confusion table tell you about the fit of this model?" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }